package com.algorithm;

/**
 * 翻转链表
 */
public class ReverseLinkedList {

    public static ListNode reverseList(ListNode head) {
        ListNode prev=null;
        ListNode curr=head;
        ListNode next=head;
        while (curr!=null){
            next=curr.next;
            curr.next=prev;
            prev=curr;
            curr=next;
        }
        //操作完 prev就到了最后一个节点位置了
        return prev;
    }

    public static void main(String[] args) {
        ListNode listNode=new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(4,new ListNode(5)))));
        ListNode node = reverseList(listNode);
        System.out.println(node);
    }
}
